﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Channels;
using FXC.SL.CS.ViewModal;
using FXC.SL.CS.Util;
using FXC.SL.CS.Modal;

namespace FXC.SL.UserControls
{
    public partial class InstantMarketQuoteGrid : UserControl
    {
        public InstantMarketQuoteGrid()
        {
            InitializeComponent();
        }

        public void Start()
        {
            FXCenterService.FXCServiceClient proxy = FXCServiceClientManager.CreateProxy();
            proxy.ReceiveMessageReceived += (o, e) =>
                {
                    if (e.Error == null)
                    {
                        InstantMarketQuoteViewModal vm = App.Current.Resources["InstantMarketQuoteViewModal"] as InstantMarketQuoteViewModal;
                        if (vm != null)
                            vm.UpdateItem(e.instantMessage);
                    }
                };
            proxy.GetMessageAsync();
        }

        #region Context Menus Events
        private void rmiNewChart_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            if (dgMarketQuote.SelectedItem != null)
            {
                MainPage mp = App.Current.RootVisual as MainPage;
                if (mp != null)
                    mp.CreateNewChart(((InstantMarketQuote)dgMarketQuote.SelectedItem).ProductName);
            }
        }
        #endregion

        #region DataGrid dgMarketQuote events
        private void dgMarketQuote_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            IEnumerable<UIElement> elementsUnderMouse =VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), this);
            DataGridRow row =elementsUnderMouse
                            .Where(uie => uie is DataGridRow)
                            .Cast<DataGridRow>()
                            .FirstOrDefault();
            if (row != null)
                dgMarketQuote.SelectedItem = row.DataContext;

        }
        #endregion
    }
}
